package com.yyq.adms.yyb.paper.action;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.yyq.adms.bzgf.pojo.StandardFormation;
import com.yyq.adms.yyb.paper.bpo.EmployPaperBpo;
import com.yyq.adms.yyb.paper.bpo.PaperSubmissionBpo;
import com.yyq.adms.yyb.paper.pojo.EmployPaper;
import com.yyq.adms.yyb.paper.pojo.PaperSubmission;
import com.yyq.adms.yyb.softwareManager.pojo.ReportCertificate;
import com.yyq.adms.yyb.utils.YYBUtils;
import com.yyq.sos.action.BaseAction;
import com.yyq.sos.constants.ActionType;
import com.yyq.sos.constants.Constants;
import com.yyq.sos.permission.LoginUser;
import com.yyq.sos.util.PermissionsUtil;
import com.yyq.sos.util.page.PageFormData;


/**
 * 投稿论文Action 层
 * @author 杨应滨
 * @2012-12-3
 *
 */
@Controller("paperSubmissionAction")
@Scope("prototype")
public class PaperSubmissionAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private PaperSubmission po = new PaperSubmission();
	
	private PaperSubmissionBpo bpo;
	
	private EmployPaperBpo employPaperBpo;
	
	

	public EmployPaperBpo getEmployPaperBpo() {
		return employPaperBpo;
	}

	@Resource(name="employPaperBpo")
	public void setEmployPaperBpo(EmployPaperBpo employPaperBpo) {
		employPaperBpo.setCls(EmployPaper.class);
		this.employPaperBpo = employPaperBpo;
	}

	public PaperSubmission getPo() {
		return po;
	}

	public void setPo(PaperSubmission po) {
		this.po = po;
	}

	public PaperSubmissionBpo getBpo() {
		return bpo;
	}

	@Resource(name="paperSubmissionBpo")
	public void setBpo(PaperSubmissionBpo bpo) {
		bpo.setCls(po.getClass());
		this.bpo = bpo;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
	public String goAdd()
	{
		String[] type = (String[])this.getFormValue("type");
		if(type != null && "employed".equals(type[0])){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			this.setFormValue("nowDate", sdf.format(new Date()));
			String sentDep = po.getSentDep();
			po = (PaperSubmission)bpo.findByPk(po.getId());
			po.setSentDep(sentDep);
			this.setObj(po);
			return "publishAdd";
		}else{
			return ActionType.ACTION_GO_ADD;
		}
	}
	
	@Override
	public String update() {
		// TODO Auto-generated method stub
		String state = this.po.getStates();
		Long sourceID = this.po.getId();
		String result =  super.update();
		String updateFlag = (String)this.getFormValue("updateFlag");
		if(state!=null && state.equals("2")&& "TRUE".equals(updateFlag)) {
			// 已录用 需保存
			String tableName = employPaperBpo.getTableName(EmployPaper.class);
			List _tempList = employPaperBpo.searchSql("select * from " + tableName + " where sourceid = " + sourceID);
			
			if(_tempList.size() == 0 ) {
				this.setMsg("{processId:\"2\",msg:\"更新成功\",sourceId:\""+sourceID+"\",sentDep:\""+po.getSentDep()+"\"}");
				
				/*EmployPaper target = new EmployPaper();
				target.setPaperName(po.getPaperName());
				target.setAuthor(po.getAuthor());
//				target.setSentDep(po.getSentDep());
				target.setBelongPlateFormID(po.getBelongPlateFormID());
				target.setSubmitDep(po.getSubmitDep());
				target.setSourceID(sourceID.intValue());
				target.setState("2");
				target.setEmployDate(new Date());
				Serializable id = employPaperBpo.save(target);
				if(id != null){
					String sentDep = po.getSentDep();
					List sds = new ArrayList();
					processSentDep(sds, tableName, sentDep, id);
					if(sds.size()>0){
						Serializable[] ids = bpo.save(sds.toArray());
					}
				}*/
			}
		}
		return result;
	}
	
	public String execute()throws Exception
	{
		super.setBpo(bpo);
		super.setObj(po);
		String s = super.execute();
		this.setPo((PaperSubmission)super.getObj());
		return s;
	}
	
	
	@Override
	public String data() {
		LoginUser user = (LoginUser)this.getSession().getAttribute(Constants.LOGIN_USER);
		String moduleName = getModuleName(this.getRequest().getRequestURI());
		boolean isPerm = PermissionsUtil.checkPermissionByCode(this.getSession(), moduleName,Constants.RIGHT_GLOBAL);
		String hql = "";
		if(user.getUserId().intValue()==1 || isPerm){
			hql = "from PaperSubmission order by remarkDate desc";
		}else{
			hql = "from "+po.getClass().getName()+" obj where (obj.maintainDep="+user.getOrgId()+" or EXISTS(from RecordSentDep rsd where obj.id=rsd.sourceRecordId and rsd.yyqTableName='"+bpo.getTableName(po.getClass())+"' and rsd.sentDepId="+user.getOrgId()+")) order by obj.remarkDate desc";
		}
		this.setFormData(bpo.searchPage(hql, getPageForm().getPageNo(), getPageForm().getPageSize()));
		
		processData();
		
		return ActionType.ACTION_DATA_LIST;
	}

	private void processData() {
		PageFormData formData= this.getFormData();
		List _templist = formData.getData();
		List _list = new ArrayList();
		for(int i = 0; i < _templist.size(); i ++) {
			PaperSubmission _declare = (PaperSubmission)_templist.get(i);
			Date _date = _declare.getRemarkDate();
			String _year = YYBUtils.getFormatDateYear(_date);
			_declare.setBelongYear(_year);
			_list.add(_declare);
		}
		if(_list.size() > 0) 
			formData.setData(_list);
	}
	
	public String search()
	{
		String searchProperty = ((String[])this.getFormValue("searchProperty"))[0];
		String searchKey = ((String[])this.getFormValue("searchKey"))[0];
		LoginUser user = (LoginUser)this.getSession().getAttribute(Constants.LOGIN_USER);
		String moduleName = getModuleName(this.getRequest().getRequestURI());
		boolean isPerm = PermissionsUtil.checkPermissionByCode(this.getSession(), moduleName,Constants.RIGHT_GLOBAL);
		String hql = "";
		if(user.getUserId().intValue()==1 || isPerm){
			if("sentDep".equals(searchProperty)){
				hql = "select distinct obj from "+po.getClass().getName()+" obj,RecordSentDep rsd,SysDepts d where " +
					  " obj.id=rsd.sourceRecordId and rsd.sentDepId=d.id and rsd.yyqTableName='"+bpo.getTableName(po.getClass())+"' and d.deptCname like '%"+searchKey+"%' order by remarkDate desc";
			}else{
				hql = "from "+po.getClass().getName()+" where "+searchProperty+" like '%"+searchKey+"%' order by remarkDate desc";
			}
		}else{
			hql = "from "+po.getClass().getName()+" obj where (obj.maintainDep="+user.getOrgId()+" or EXISTS(from RecordSentDep rsd where obj.id=rsd.sourceRecordId and rsd.yyqTableName='"+bpo.getTableName(po.getClass())+"' and rsd.sentDepId="+user.getOrgId()+")) and obj."+searchProperty+" like '%"+searchKey+"%' order by obj.remarkDate desc";
		}
		this.setFormData(bpo.searchPage(hql, this.getPageForm().getPageNo(), this.getPageForm().getPageSize()));
		processData();
		return ActionType.ACTION_DATA_LIST;
	}
}
